import instance from '@/http/axios'
import {Tokens} from '@/stores/token'

// const ticketStore = useTicketStore()

interface responseInter {
    success: boolean
    info: string
    result: any
    code: string
}

const request = async ({
                           method,
                           url,
                           data,
                           config,
                           // loading = false,
//接口地址上有没有api
//                            withoutApiString = false,
                       }: any): Promise<responseInter> => {
    method = method.toLowerCase()
    // const tokens = Tokens()
    //实时获取新的token
    // 设置axios实例请求默认请求头中Authorization的值
    // instance.defaults.headers['Authorization'] = tokens.getToken()

    // if (withoutApiString === false) url = 'api' + url
    if (method == 'post') {
        return instance.post(url, data, {...config},)
    } else if (method == 'get') {
        return instance.get(url, {
            params: data,
            ...config
        })
    } else if (method == 'delete') {
        return instance.delete(url, {
            params: data,
            ...config
        })
    } else if (method == 'put') {
        return instance.put(url, data, {...config})
    } else {
        console.error('未知的method' + method)
        return {
            success: false,
            info: '未知method',
            result: {},
            code: 'fail',
        }
    }
};

instance.interceptors.request.use(function (config) {
    // 在发送请求之前做些什么
    // console.log("发送请求了");
    const tokens = Tokens()
    config.headers['Authorization'] = tokens.getToken()
    // console.log(config)
    return config;
}, function (error) {
    // 对请求错误做些什么
    return Promise.reject(error);
});

export {
    request,
    type responseInter,
}